<?php
/* OpenBIBLIO -- A library administration web interface
 * Copyright (C) 2002-2005 Fr&eacute;d&eacute;ric Descamps <lefred@tiscali.be>
 * Copyright (C) 2011-     Laurent Luyssen   <big.lol13@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	$sw					= $_POST['sw'];
	$liv_nom    		= format_title($_POST['liv_nom']);
	$liv_auteur   		= format_author($_POST['liv_auteur']);
	$liv_col			= $_POST['liv_col'];
	$liv_genre_id		= $_POST['liv_genre_id'];
	$liv_etat_id		= $_POST['liv_etat_id'];
	$liv_location_id 	= $_POST['liv_location_id'];
	$liv_ed				= $_POST['liv_ed'];
	$liv_ISBN			= $_POST['liv_ISBN'];
	$liv_num			= $_POST['liv_num'];
	$resume				= $_POST['resume'];
	$motclefs			= $_POST['motclefs'];
	$temp				= $_POST['temp'];
	$noout				= $_POST['noout'];
	$liv_date			= $_POST['liv_date'];
	$cover_url			= $_POST['liv_url'];
}
else
	$sw = 0;

if($sw==1) {                       
	if($temp != 1)
		$temp = 0;
	if ($noout != 1)
		$noout = 0;
	$date_in = date("Y-m-d");
	$requete = "insert into livres (nom,auteur,collection,genre_ID,etat_ID,location_ID,edition,date_ed,ISBN,temp,noout,date_in,resume,motclefs,num, cover_url) 
		values ('$liv_nom', '$liv_auteur', '$liv_col', $liv_genre_id, $liv_etat_id, $liv_location_id, '$liv_ed', '$liv_date', '$liv_ISBN', $temp, $noout, '$date_in', '$resume', '$motclefs', '$liv_num', '$cover_url')";
	if (!($resultat = biblio_db_query($requete))) {
		echo "PROBLEME :" .biblio_db_error();  
		echo "</BR> requete = ".$requete; 
		exit;
	} 
	put_log(2,$requete,$user);                                                  
	$filename = biblio_db_insert_id() . ".jpg";
	$cover_path = get_coverspath();
	if ($_FILES['liv_cover']['tmp_name']) {
		move_uploaded_file($_FILES['liv_cover']['tmp_name'], $cover_path.$filename);
	}
	echo "<center><br><b>$liv_nom</b> ";
	echo _("est ajout&eacute; &agrave; la base de donn&eacute;es.");
	echo "<br><br><a href=\"admin.php?action=add&type=livre\">";
	echo _("ajouter un autre livre");
	echo "</a><br></center>";
}
else {
	$locations = get_locations();
	if($locations && count($locations) > 0) {
?>

<script type="text/javascript" src="js/bsn.AutoSuggest_2.1.3_min.js" charset="utf-8"></script>
<link rel="stylesheet" href="css/autosuggest_inquisitor_min.css" type="text/css" media="screen" charset="utf-8" />

<center><H3><?php echo _("Ajouter un livre"); ?></H3>
	<form action="admin.php" METHOD="POST" id ="form1" name="form1">
        <table border=0>
        <tr>
			<td><?php echo _("Titre"); ?> :</td>
			<td><input type="text" name="liv_nom" id="liv_nom" size="40" value=""/></td>
			<td rowspan="7">
<?php if (isAmazonEnabled()) { ?>
				<img name="liv_img" id="liv_img" src="images/livre.jpg"/>
<?php } ?>
			</td>

		</tr>
        <tr>
			<td><?php echo _("Num&eacute;ro"); ?> :</td>
			<td><input type="text" name="liv_num" id="liv_num" size="4"/></td>
		</tr>
        <tr>
			<td><?php echo _("Auteur"); ?> :</td>
			<td><input type="text" name="liv_auteur" id="liv_auteur" size="40" value=""/></td>
		</tr>
        <tr>
			<td><?php echo _("Collection"); ?>	:</td>
			<td><input type="text" name="liv_col" id="liv_col" size="40"/></td>
		</tr>
        <tr>
			<td><?php echo _("Edition"); ?> :</td>
			<td><input type="text" name="liv_ed" id="liv_ed" size="30" value=""/></td>
		</tr>
        <tr>
			<td><?php echo _("Date"); ?> :</td>
			<td><input type="text" name="liv_date" id="liv_date" size="10" value=""/></td>
		</tr>
        <tr>
			<td><?php echo _("Genre"); ?> :</td>
			<td>
				<select name="liv_genre_id">
<?php
		$genres = get_genres();
		if ($genres) {
			foreach ($genres as $key => $value) 
				print "<option VALUE=\"$key\">$value</option>";
		}
?>
				</select>
			</td>
		</tr>
        <tr>
			<td><?php echo _("ISBN"); ?> :</td>
			<td>
				<input type="text" name="liv_ISBN" id="liv_ISBN" size="13" value=""/>
<?php if (isAmazonEnabled()) { ?>
				<input type="button" value="<?php echo _("Recherche Amazon"); ?>" onClick='document.jsap.searchIsbn(document.forms["form1"].liv_ISBN.value);'/>
<?php } ?>
			</td>
			<td>
<?php if (isAmazonEnabled()) { ?>
				<applet  name="OpenBiblio Isbn Search Applet" id="jsap"
					code="com.biglol.openbiblio.IsbnSearchApplet" 
					archive="applet/ProductAdvertisingAPI.jar, applet/lib/netscape.jar" width="16" height="16" MAYSCRIPT >
					<param name="awsAccessKeyId" value="<?php echo getAmazonKeyId(); ?>"/>
					<param name="awsSecretKey" value="<?php echo getAmazonSecret(); ?>"/>
					<param name="awsAssociateTag" value="<?php echo getAmazonAssociateTag(); ?>"/>
				</applet>
<?php } ?>
			</td>
		</tr>
		<tr>
			<td><?php echo _("R&eacute;sum&eacute;"); ?> :</td>
			<td colspan="2"><TEXTAREA name="resume" id="resume" cols="50" rows="5"></TEXTAREA></td>
		</tr>
		<tr>
			<td><?php echo _("Mots cl&eacute;"); ?> :</td>
			<td colspan="2">
				<input type="text" name="motclefs" id="motclefs" size="40" maxlength="200"/>
				<small>(<?php echo _("s&eacute;par&eacute;s par un espacement"); ?>	)</small>
			</td>
		</tr>
        <tr>
			<td><?php echo _("Etat"); ?> :</td>
			<td colspan="2">
				<select name="liv_etat_id">                                
<?php
		$etats =get_etats();
		if ($etats) {
			$selected = 'selected="selected"';
			foreach ($etats as $id => $value)  {
				print "<option VALUE=\"$id\" $selected>$value</option>";
				$selected = "";
			}
		}
?>
				</select>
			</td>
		</tr>
        <tr>
			<td><?php echo _("Location"); ?> :</td>
			<td colspan="2">
				<select name="liv_location_id">
<?php
		if ($locations) {
			foreach ($locations as $id => $location) {
				echo '<option VALUE="'.$id.'">';
				echo $location.'</option>';
			}
		}
?>
				</select>
			</td>
		</tr>
        <tr>
			<td><?php echo _("Temporaire"); ?> :</td>
			<td colspan="2"><input type="checkbox" name="temp" id="temp" value="1"/></td>
		</tr>
        <tr>
			<td><?php echo _("Ne peux sortir !"); ?> :</td>
			<td colspan="2"><input type="checkbox" name="noout" id="noout" value="1"/></td>
		</tr>
		<tr>
			<td><?php echo _("Couverture"); ?> :</td>
			<td colspan="2">
				<input type="file" name="liv_cover" id="liv_cover"/>
				<i>	<?php echo _("fichier"); ?> </i><br>
				<input type="text" size="40" name="liv_url" id="liv_url" value=""/>
				<i>	<?php echo _("ou url"); ?> 	</i>
			</td>
		</tr>
	</table>
	<br>
<input type="hidden" name="user" value="<?php echo $user; ?>"/>
<input type="hidden" name="action" value="<?php echo $action; ?>"/>
<input type="hidden" name="type" value="<?php echo $type; ?>"/>
<input type="hidden" name="sw" value="1"/>
<input type="Submit" value="<?php echo _("Enregistrer"); ?>	"/>
</form>
</center>
        <?php
	}
	else {
		echo "<center><h3>";
		echo _("Vous devez d&eacute;finir au moins un emplacement avant d'encoder un nouveau livre !");
		echo "</center>";
	}
}
?>              
<br/><br/>
<div>
	<span id=left-text>
		<a href="admin.php?menu=1"><?php echo _("retour menu administration"); ?></a><br/>
		<a href="index.php"><?php echo _("retour menu principal"); ?></a>
	</span>
</div>
<br/>

<script language="javascript">
	function updateTitle(s) {
		document.forms["form1"].liv_nom.value = s;
	}
	function updateAuthor(s) {
		document.forms["form1"].liv_auteur.value = s;
	}
	function updatePublisher(s) {
		document.forms["form1"].liv_ed.value = s;
	}
	function updatePubDate(s) {
		document.forms["form1"].liv_date.value = s;
	}
	function updateCoverUrl(s) {
		document.forms["form1"].liv_url.value = s;
		document.forms["form1"].liv_img.src = s;
	}
	
	window.onload = function() {
		new biblioSuggest('liv_nom', 2500);
		new biblioSuggest('liv_auteur', 2500);
		new biblioSuggest('liv_ISBN', 2500);
		new biblioSuggest('liv_ed', 2500);
		new biblioSuggest('liv_col', 2500);
    }

</script>
